var express = require('express');
var router = express.Router();
let mysql = require("mysql");

const db_config = {
    host: "localhost",
    user: "root",
    password: "2018213562",
    port: "3360",
    database: "park"
}
let connect = mysql.createConnection(db_config);
//开始链接数据库
connect.connect(function (err) {
    if (err) {
        console.log(`mysql连接失败: ${err}!`);
    } else {
        console.log("mysql连接成功!");
    }

})
let sqlresult = null;
let parkSqlResult = null;
let sqlQuery = "select parkID,parkName,ST_X(parkLocation) as 'lng',ST_Y(parkLocation) as 'lat' from parkinfo";
let parksqlQuery = "select * from parkres";
// 取得停车场数据
connect.query(sqlQuery, function (err, result) {
    if (err) {
        console.log(`SQL error: ${err}!`);
       
    } else {
        console.log(result);
        sqlresult = result;

    }
});
//取得剩余车位数据
connect.query(parksqlQuery, function (err, result) {
    if (err) {
        console.log(`SQL error: ${err}!`);
    } else {

        parkSqlResult = result;
        console.log(parkSqlResult);
    }
});





router.get('/', function (req, res, next) {
    let data = {
        sqlresult,
        parkSqlResult,
    }

    res.json(data);

    console.log(res.data(data))



});


function closeMysql(connect) {
    connect.end((err) => {
        if (err) {
            console.log(`mysql关闭失败:${err}!`);
        } else {
            console.log('mysql关闭成功!');
        }
    });

}

//关闭数据库
closeMysql(connect);




module.exports = router;